
package com.cuit.xsc.weishare.activity;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.adapter.PrivateMsgAdapter;
import com.cuit.xsc.weishare.adapter.WeiboAdapter;
import com.cuit.xsc.weishare.logic.MainService;
import com.cuit.xsc.weishare.logic.Task;
import com.cuit.xsc.weishare.model.DirectMessage;
import com.cuit.xsc.weishare.util.LogUtil;
import com.cuit.xsc.weishare.widget.PullToRefreshListView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class MsgAct extends BaseActivity
{

	private TextView titleTv;
	
	//加载View
	private View progresView;
	
	private PullToRefreshListView msgListView;
	
	private PrivateMsgAdapter adapter;
	
	private Map<String,List<DirectMessage>> map;
	
	private List<DirectMessage> list;

	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.msg_);
		
		titleTv = (TextView) findViewById(R.id.txt_wb_title);
		titleTv.setText("信息");
		
		progresView = this.findViewById(R.id.layout_progress);	
		
		msgListView = (PullToRefreshListView) this.findViewById(R.id.lv_msgs);
		
		
		progresView.setVisibility(View.VISIBLE);
		
		msgListView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				if (list != null)
				{
					DirectMessage directMessage = list.get((arg2-1));
//					String key = directMessage.getName();
					String key = directMessage.getToname();
					if (key == null || "".equals(key))
					{
						key = directMessage.getName();
					}
					ArrayList<DirectMessage> list = (ArrayList<DirectMessage>) map.get(key);
					
					Intent intent = new Intent();
					intent.putExtra("list", list);
					intent.setClass(MsgAct.this, PrivateMsgConversationAct.class);
					//LogUtil.i(this, directMessage.getName());
					startActivity(intent);
							
				}
				
			}
		});
		
		Task task = new Task(Task.GET_PRIVATE_MESSAGE, null);
		MainService.addTask(task);
		//LogUtil.i(this, "progresView="+progresView);
	//	LogUtil.i(this, "msgListView="+msgListView);
	}
	
	@Override
	public void refresh(Object... params)
	{
		System.out.println("Msg refresh");
		progresView.setVisibility(View.GONE);
		
		int flag = ((Integer) params[0]).intValue();
		switch (flag)
		{
		case Task.GET_PRIVATE_MESSAGE:
			Map<String,List<DirectMessage>> map = (Map<String, List<DirectMessage>>) params[1];
			/*Iterator<String> iterator = map.keySet().iterator();
			while (iterator.hasNext())
			{
				System.out.println(iterator.next());
			}*/
			
			resoveDir(map);
			getDir();
			adapter = new PrivateMsgAdapter(this);
			if (list != null && !list.isEmpty())
			{
				adapter.setList(list);
				adapter.setListView(msgListView);
				msgListView.setAdapter(adapter);
			}
			break;
		
		
		
		
		}
	}
	
	
	private void resoveDir(Map<String,List<DirectMessage>> cmap)
	{
		if (cmap != null)
		{
			Set<String> set = cmap.keySet();
			for (String key : set)
			{
				if (map == null)
				{
					map = new HashMap<String, List<DirectMessage>>();
				}				
				List<DirectMessage> list = map.get(key);
				if (list == null)
				{
					list = new ArrayList<DirectMessage>();
					map.put(key, list);
				}
				
				list.addAll(cmap.get(key));
			}

		}
	}
	
	private void getDir()
	{
		if (map != null)
		{
			Set<String> keys = map.keySet();
			if (list == null)
			{
				list = new ArrayList<DirectMessage>();
			}
			list.clear();
			for (String key : keys)
			{
				list.add(map.get(key).get(0));
			}	
			
		}
	}
	
	
}
